﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using Microsoft.Health;
using Microsoft.Health.Events;
using Microsoft.Health.ItemTypes;
using Microsoft.Health.Web;

public partial class Update : HealthServicePage
{
    SubscriptionManager _manager;
    Subscription _subscription;

    protected void Page_Load(object sender, EventArgs e)
    {
        _manager = new SubscriptionManager(ApplicationConnection);

        title.Text = Request.QueryString["Operation"];
        string subscriptionIdString = Request.QueryString["Id"];

        if (subscriptionIdString == null)
        {
            return;
        }
        Guid subscriptionId = new Guid(subscriptionIdString);

        _subscription = SubscriptionManagerExtension.GetSubscription(_manager, subscriptionId);

        List<KeyValuePair<Guid, Type>> registeredTypes = new List<KeyValuePair<Guid, Type>>(ItemTypeManager.RegisteredTypes);

        registeredTypes.Sort(
            delegate(KeyValuePair<Guid, Type> type1, KeyValuePair<Guid, Type> type2)
            {
                return type1.Value.Name.CompareTo(type2.Value.Name);
            }
        );

        if (c_AllTypes.Items.Count == 0)
        {
            foreach (KeyValuePair<Guid, Type> item in registeredTypes)
            {
                ListItem listItem = new ListItem(item.Value.Name, item.Key.ToString());
                c_AllTypes.Items.Add(listItem);
            }
        }

        string key = Convert.ToBase64String(_subscription.CommonSubscriptionData.SharedKeyInfo.Key);
        c_notificationKey.InnerText = key;

        if (!IsPostBack)
        {
            c_textboxUrl.Text = _subscription.CommonSubscriptionData.NotificationChannel.ToString();
            c_notificationVersionId.Text = _subscription.CommonSubscriptionData.SharedKeyInfo.Version;
        }

        c_linkText.NavigateUrl = "SendTestNotification.aspx?Id=" + subscriptionIdString;
    }

    protected void Page_Prerender(object sender, EventArgs e)
    {

        c_subscriptionType.InnerText = _subscription.GetType().Name;

        HealthRecordItemChangedSubscription healthRecordItemChangedSubscription =
            _subscription as HealthRecordItemChangedSubscription;

        if (healthRecordItemChangedSubscription != null)
        {
            _DataTypeTable.DataSource = healthRecordItemChangedSubscription.Filters[0].TypeIds;
            _DataTypeTable.DataBind();
        }
    }


    protected void linkDeleteClick(Object sender, CommandEventArgs e)
    {
        Guid id = new Guid((string)e.CommandArgument);

        HealthRecordItemChangedSubscription healthRecordItemChangedSubscription =
            _subscription as HealthRecordItemChangedSubscription;

        IList<Guid> typeIds = healthRecordItemChangedSubscription.Filters[0].TypeIds;

        if (healthRecordItemChangedSubscription != null)
        {
            for (int i = typeIds.Count - 1; i >= 0; i--)
            {
                if (typeIds[i] == id)
                {
                    typeIds.RemoveAt(i);
                }
            }
        }
        _manager.UpdateSubscription(_subscription);
    }

    protected void addClick(Object sender, EventArgs e)
    {
        SubscriptionManager manager = new SubscriptionManager(null);

        ListItem item = c_AllTypes.SelectedItem;

        Guid typeId = new Guid(item.Value);

        HealthRecordItemChangedSubscription healthRecordItemChangedSubscription =
            _subscription as HealthRecordItemChangedSubscription;

        healthRecordItemChangedSubscription.Filters[0].TypeIds.Add(typeId);
        _manager.UpdateSubscription(_subscription);
    }


    protected void urlChanged(Object sender, EventArgs e)
    {
        HttpNotificationChannel channel =
            _subscription.CommonSubscriptionData.NotificationChannel as HttpNotificationChannel;

        channel.Url = new Uri(c_textboxUrl.Text);
        _subscription.CommonSubscriptionData.SharedKeyInfo.Version = c_notificationVersionId.Text;

        _manager.UpdateSubscription(_subscription);
    }
}